namespace MouseGestures
{
    using System.Collections.Generic;
    using System.Drawing;

    /// <summary>
    /// 	Enum defining gesture directions.
    /// </summary>
    public enum MouseGestureDirection
    {
        Unknown,

        Up,

        Right,

        Down,

        Left
    }

    /// <summary>
    /// 	A MouseGesture is a sequence of movements
    /// </summary>
    public class MouseGesture
    {
        #region Fields

        private readonly List<MouseGestureDirection> directions;

        private readonly Point start;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 	Create a empty mouse gesture
        /// </summary>
        /// <param name="start"> The point where the gesture started </param>
        public MouseGesture(Point point)
        {
            this.start = point;
            this.directions = new List<MouseGestureDirection>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// 	The number of motions in the gesture
        /// </summary>
        public int Count
        {
            get
            {
                return this.directions.Count;
            }
        }

        /// <summary>
        /// 	Gets the performed gesture
        /// </summary>
        public string Motions
        {
            get
            {
                return this.ToString();
            }
        }

        /// <summary>
        /// 	The point where the gesture started
        /// </summary>
        public Point Start
        {
            get
            {
                return this.start;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 	Append a motion to the gesture
        /// </summary>
        /// <remarks>
        /// 	Duplicate and unknown motions will be filtered out.
        /// </remarks>
        public void AddSegment(MouseGestureDirection direction)
        {
            if (direction != MouseGestureDirection.Unknown
                && (this.directions.Count == 0 || direction != this.directions[this.directions.Count - 1]))
            {
                this.directions.Add(direction);
            }
        }

        /// <summary>
        /// 	A string representation of the gesture
        /// </summary>
        public override string ToString()
        {
            string s = string.Empty;
            foreach (var d in this.directions)
            {
                switch (d)
                {
                    case MouseGestureDirection.Left:
                        s += 'L';
                        break;
                    case MouseGestureDirection.Right:
                        s += 'R';
                        break;
                    case MouseGestureDirection.Up:
                        s += 'U';
                        break;
                    case MouseGestureDirection.Down:
                        s += 'D';
                        break;
                    case MouseGestureDirection.Unknown:
                        break;
                }
            }
            return s;
        }

        #endregion
    }
}